﻿/****************************** Module Header ******************************\
 * Module Name:  SocketHelper.cs
 * Module Version:  1.0.0 (2012-2-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to send email by Net MailMessage dll. It supplies 
 * useful method to send a email by SMTP server.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;

namespace UCF.Core
{
    /// <summary>
    /// Socket帮助类
    /// </summary>
    public class SocketHelper
    {
        /// <summary>
        /// 服务器IP
        /// </summary>
        public string ServerIP { get; set; }

        /// <summary>
        /// 服务器端口
        /// </summary>
        public int ServerPort { get; set; }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message"></param>
        public void Send(string message)
        {
            /*
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(this.ServerIP);
            IPEndPoint iep = new IPEndPoint(ip, this.ServerPort);
            socket.Connect(iep);
            socket.Send(Encoding.Default.GetBytes(message));
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
             */
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        /// <returns></returns>
        public string Receive()
        {
            return "";
            /*
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(this.ServerIP);
            IPEndPoint iep = new IPEndPoint(ip, this.ServerPort);
            byte[] byteMessage = new byte[255];
            socket.Bind(iep);
            socket.Listen(5);
            Socket subSocket = socket.Accept();
            subSocket.Receive(byteMessage);
            return Encoding.Default.GetString(byteMessage);
             */
        }
        /// <summary>
        /// 侦听
        /// </summary>
        public void Listen()
        {
            /*
            IPAddress ipAddress = IPAddress.Any; //IPAddress.Parse("172.16.102.11");
            TcpListener tcpListener = new TcpListener(ipAddress, 5009);
            tcpListener.Start();
            TcpClient tcpClient = tcpListener.AcceptTcpClient();
            NetworkStream ns = tcpClient.GetStream();
            StreamReader sr = new StreamReader(ns);
            string result = sr.ReadToEnd();

            tcpClient.Close();
            tcpListener.Stop();
             */
        }
        /// <summary>
        /// 侦听
        /// </summary>
        public void SocketListen()
        {
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(new IPEndPoint(IPAddress.Any, 5009));
            listener.Listen(0);

            Socket socket = listener.Accept();
            Stream netStream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(netStream);

            string result = reader.ReadToEnd();

            socket.Close();
            listener.Close();
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="content"></param>
        public void Send(string ip, string port, string content)
        {

            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(IPAddress.Parse(ip), Int32.Parse(port));

            NetworkStream ns = tcpClient.GetStream();
            if (ns.CanWrite)
            {
                Byte[] sendBytes = Encoding.UTF8.GetBytes(content);
                ns.Write(sendBytes, 0, sendBytes.Length);
            }
            else
            {
                tcpClient.Close();

                ns.Close();
                return;
            }
            ns.Close();
            tcpClient.Close();
        }
    }
}
